﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Ecreate.Model;
using Ecreate.Data;
namespace Ecreate.Business
{
    public class SystemUserService
    {
        public WebResult<t_systemuser> CheckUser(t_systemuser systemuser)
        {
            try
            {
                WebResult<t_systemuser> result = new WebResult<t_systemuser>();
                object o = FactoryDBEngine.getIBatisService().QueryForObject("t_systemuser_SelectByID", systemuser.Userid);
                t_systemuser searchuser = (t_systemuser)o;
                if (searchuser == null)
                {
                    result.Message = "用户不存在!";
                    result.Success = false;
                }
                else
                {
                    if (searchuser.Password.Equals(systemuser.Password))
                    {
                        result.Success = true;
                        result.Message = "sucess";
                        result.ResultObject = searchuser;
                    }
                    else
                    {
                        result.Success = false;
                        result.Message = "密码错误!";
                    }
                }
                return result;
            }
            catch(Exception ex)
            {
                WebResult<t_systemuser> result = new WebResult<t_systemuser>();
                result.Success = false;
                result.Message = ex.Message;
                result.Message = result.Message.Replace("'", "");
                result.Message = result.Message.Replace("\"", "");
                result.Message = result.Message.Replace("/", "");
                result.Message = result.Message.Replace("\\", "");
                result.Message = result.Message.Replace(">", "");
                result.Message = result.Message.Replace("<", "");
                result.Message = result.Message.Replace("-", "");
                return result;
            }
        }
        public ArrayList ListAll()
        {
           object o=FactoryDBEngine.SqlMapper().QueryForList("t_systemuser_SelectAll", null);
           return (ArrayList)o;
        }

        public WebResult<object> Delete(string id)
        {
            WebResult<object> result = new WebResult<object>();
            t_systemuser search = FactoryDBEngine.SqlMapper().QueryForObject<t_systemuser>("t_systemuser_SelectByID", id);
            if (search == null)
            {
                result.Message="删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                if (search.Adminflag == 1)
                {

                    result.Success = false;
                    result.Message = "超级管理员不允许删除!";
                }
                else
                {
                    FactoryDBEngine.SqlMapper().Delete("t_systemuser_Delete", id);
                    result.Success = true;
                    result.Message = "删除成功!";
                }
            }
            return result;

        }
        public WebResult<Object> Insert (t_systemuser model)
        {
            WebResult<object> result = new WebResult<object>();
            t_systemuser search = FactoryDBEngine.SqlMapper().QueryForObject<t_systemuser>("t_systemuser_SelectByID", model.Userid);
            if (search != null)
            {
                result.Message = "管理员ID重复";
                result.Success = false;
            }
            else
            {

                {
                    FactoryDBEngine.SqlMapper().Insert("t_systemuser_Insert", model);
                    result.Success = true;
                    result.Message = "删除成功!";
                }
            }
            return result;

        }

        public WebResult<object> Update(t_systemuser model)
        {
            WebResult<object> result = new WebResult<object>();
            t_systemuser search = FactoryDBEngine.SqlMapper().QueryForObject<t_systemuser>("t_systemuser_SelectByID", model.Userid);
            if (search == null)
            {
                result.Message = "删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                if (search.Adminflag == 1)
                {
                    result.Success = false;
                    result.Message = "超级管理员不允许修改!";
                }
                else
                {
                    FactoryDBEngine.SqlMapper().Update("t_systemuser_Update", model);
                    result.Success = true;
                    result.Message = "删除成功!";
                }
            }
            return result;

        }
        public WebResult<t_systemuser> getByUserID(string id)
        {
            WebResult<t_systemuser> result = new WebResult<t_systemuser>();
            t_systemuser search = FactoryDBEngine.SqlMapper().QueryForObject<t_systemuser>("t_systemuser_SelectByID", id);
            if (search == null)
            {
                result.Message = "删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                result.Success = true;
                result.ResultObject = search;
            }
            return result;

        }


        public WebResult<object> UpdatePassword(t_systemuser model)
        {
            WebResult<object> result = new WebResult<object>();
            t_systemuser search = FactoryDBEngine.SqlMapper().QueryForObject<t_systemuser>("t_systemuser_SelectByID", model.Userid);
            if (search == null)
            {
                result.Message = "删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                {
                    FactoryDBEngine.SqlMapper().Update("t_systemuser_UpdatePassword", model);
                    result.Success = true;
                    result.Message = "修改密码成功!";
                }
            }
            return result;

        }

    }
}
